package com.yl.chainStore.control.sick

import com.yl.chainStore.common.Constants
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.sick.SickCustomerImage
import com.yl.chainStore.pojo.vo.sick.SickCustomerImageListSaveVo
import com.yl.chainStore.pojo.vo.sick.SickCustomerImageResultVo
import com.yl.chainStore.pojo.vo.sick.SickCustomerImageSelectVo
import com.yl.chainStore.services.ICompanyDictionaryService
import com.yl.chainStore.services.ICustomerInfoService
import com.yl.chainStore.services.sick.ISickMedicalHistoryService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 客户图片界面
 */
@RestController
@RequestMapping("sick/image")
open class SickCustomerImageController(
	private val iCustomerInfoService: ICustomerInfoService,
	private val iCompanyDictionaryService:ICompanyDictionaryService,
	private val iSickMedicalHistoryService: ISickMedicalHistoryService
) : BaseCtrl() {

	/**
	 *  获取病人图片
	 */
	@PostMapping("getImageList")
	@PermsCode("customer:sick:image:show")
	@Transactional
	open fun getImageList(@RequestBody vo: SickCustomerImageSelectVo): ResponseBody {
		log.info("客户图片页面 ==> 查询客户图片 vo:{}",vo)
		if (vo.customerId == null) {
			return returnError(ErrorCode.PARAM_EMPTY, "请选择客户")
		}
		return returnSuccess(imageListToDayList(iCustomerInfoService.getImageList(vo.customerId!!, vo)))
	}

	/**
	 * 图片转成 day-list 格式
	 */
	private fun imageListToDayList(list:List<SickCustomerImage>):List<SickCustomerImageResultVo>{
		val resList = mutableListOf<SickCustomerImageResultVo>()
		val sdf = SimpleDateFormat("yyyy-MM-dd")
		var nowDay = ""
		var resObj:SickCustomerImageResultVo ?= null
		for(item in list){
			val imageDay = sdf.format(item.createTime)
			if(!imageDay.equals(nowDay)){
				if(resObj != null){
					resList.add(resObj)
				}
				resObj = SickCustomerImageResultVo(day = imageDay)
				nowDay = imageDay
			}
			resObj!!.list.add(item)
		}
		if(resObj != null){
			resList.add(resObj)
		}

		return resList
	}



	/**
	 *  添加图片
	 */
	@PostMapping("addImage")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("customer:sick:image:add")
	open fun addImage(@RequestBody image: SickCustomerImage): ResponseBody {
		val addImage = iCustomerInfoService.addImage(image)
		if(addImage.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return addImage
		}
		if(image.medicalHistoryId != null){
			val bindImageToHistory = iSickMedicalHistoryService.bindImageToHistory(image.medicalHistoryId!!, image.id)
			if(bindImageToHistory.bodyIsError()){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
				return bindImageToHistory
			}
		}
		return returnSuccess()
	}

	/**
	 *  添加图片(批量)
	 */
	@PostMapping("addImageList")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("customer:sick:image:add")
	open fun addImageList(@RequestBody vo: SickCustomerImageListSaveVo): ResponseBody {
		if(vo.images == null || vo.images!!.isEmpty()){
			return returnError(ErrorCode.PARAM_EMPTY,"请上传图片")
		}
		for(imageVo in vo.images!!){
			val image = SickCustomerImage(
				id = 0L,
				remark = vo.remark,
				customerId = vo.customerId,
				medicalHistoryId = vo.medicalHistoryId,
				typeId = vo.typeId,
				url = imageVo.url,
				name = imageVo.name,
				localPath = imageVo.localPath,
				createTime = vo.createTime?: Date()
			)
			val addImage = iCustomerInfoService.addImage(image)
			if(addImage.bodyIsError()){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
				return addImage
			}
			if(image.medicalHistoryId != null){
				val bindImageToHistory = iSickMedicalHistoryService.bindImageToHistory(image.medicalHistoryId!!, image.id)
				if(bindImageToHistory.bodyIsError()){
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
					return bindImageToHistory
				}
			}
		}
		return returnSuccess()
	}

	/**
	 *  删除图片
	 */
	@PostMapping("delImage/{id}")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("customer:sick:image:del")
	open fun delImage(@PathVariable id: Long): ResponseBody {
		val delImage = iCustomerInfoService.delImage(id)
		if(delImage.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return delImage
		}
		return returnSuccess()
	}



	@GetMapping("getCustomerImageType")
	@Transactional
	open fun getCustomerImageType():ResponseBody{
		return returnSuccess(
			iCompanyDictionaryService.getCompanyDetailListToMapIdName(
				Constants.DistId.CUSTOMER_IMAGE_TYPE,
				getContext().companyId,
				getContext().subCompanyId
			)
		)
	}


}
